package org.jsoft.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.jsoft.bean.MingxiBean;
import org.jsoft.bean.UserBean;
import org.jsoft.dao.UserDao;


/**
 * Servlet implementation class UserServlet
 */
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		UserServlet us=new UserServlet();
		us.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=utf-8");
		PrintWriter out=response.getWriter();
		UserDao ud=new UserDao();
		String instatus=request.getParameter("instatus");
		if("login".equals(instatus)){  //登陆
			String webname=request.getParameter("wname");
			String webpass=request.getParameter("wpass");
			UserBean ub=ud.login0(webname, webpass);
			Date dt = new Date();
			SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String ltime = d.format(dt);
			int i=ud.login(webname, webpass);
			switch(i){
			case 0:
				out.print(0); //没有该用户
				break;
			case 1:
				out.print(1); //密码输入错误
				break;
			case 2:
				request.getSession().setAttribute("cardid", ub.getCardid());
				request.getSession().setAttribute("name", ub.getName());
				request.getSession().setAttribute("balance", ub.getBalance());
				request.getSession().setAttribute("welcome", ub.getWelcome());
				request.getSession().setAttribute("logintime", ltime);
				request.getSession().setAttribute("webid", ub.getWebid());
				request.getSession().setAttribute("phone", ub.getPhone());
				out.print(2);
				break;
			case 3:
				out.print(3); //密码输入错误
				break;
			case 4:
				request.getSession().setAttribute("cardid", ub.getCardid());
				request.getSession().setAttribute("name", ub.getName());
				request.getSession().setAttribute("balance", ub.getBalance());
				request.getSession().setAttribute("welcome", ub.getWelcome());
				request.getSession().setAttribute("logintime", ltime);
				request.getSession().setAttribute("webid", ub.getWebid());
				request.getSession().setAttribute("phone", ub.getPhone());
				out.print(4);
				break;
			}
		}
		if("register".equals(instatus)){ //注册第一步
			String cardid=request.getParameter("cardid");
			request.getSession().setAttribute("cardid", cardid);
			String userid=request.getParameter("userid");
			String pass=request.getParameter("pass");
			int i=ud.register1(cardid, userid, pass);
			switch(i){
			case -1:
				out.print(-1);
				break;
			case 0:
				out.println(0); //卡号不存在
				break;
			case 1:
				out.println(1);  //身份证号和卡号不匹配
				break;
			case 2:
				out.println(2);  //取款密码输入错误
				break;
			case 3:
				out.print(3);  //成功，进入下一步
				break;
			}
		}else if("register_next".equals(instatus)){//注册第二步
			String webname=request.getParameter("webname");
			String webpass1=request.getParameter("webpass1");
			String webpass2=request.getParameter("webpass2");
			int i=0;
			if(webname==null||webname==""){
				i=2; //用户名不可为空
			}else if(webpass1==null||webpass1==""||webpass2==null||webpass2==""){
				i=3;//密码不可为空
			}else if(!webpass1.equals(webpass2)){
				i=0;
			}else{
				String cardid=(String)(request.getSession().getAttribute("cardid"));  //获取session值，并强制类型转换
				i=ud.register2(cardid, webname, webpass1);
				UserBean ub=ud.register_login(cardid);
				request.getSession().setAttribute("name", ub.getName());
				request.getSession().setAttribute("webid", ub.getWebid());
				request.getSession().setAttribute("welcome", ub.getWelcome());
				Date dt = new Date();
				SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String ltime = d.format(dt);
				request.getSession().setAttribute("logintime", ltime);
			}
			switch(i){
			case 1:
				out.println(1);//注册成功
				break;
			case 0:
				out.print(0);//两次密码输入不一致
				break;
			case 2:
				out.print(2);//用户名不可为空
				break;
			case 3:
				out.print(3);
				break;
			}
		}else if("zhuanzhang".equals(instatus)){ //转账
			String cardid=(String)(request.getSession().getAttribute("cardid"));//获取session值，即当前卡号
			String tcardid=request.getParameter("skid");  //获取对方卡号
			String mon=request.getParameter("money");//获取用户填写的转账金额
			UserBean ub0=new UserBean();
			ub0=ud.transfer0(tcardid);
			request.getSession().setAttribute("tname", ub0.getName());
			JSONObject job=new JSONObject();
			job.put("tname", request.getSession().getAttribute("tname"));
			double login_balance=(double)(request.getSession().getAttribute("balance"));
			double money=Double.parseDouble(mon);  //将String类型转换成double类型
			double balance=login_balance-money*(1+0.005);
			request.getSession().setAttribute("balance", balance);
			int i=ud.transfer(cardid, tcardid, money);
			switch (i) {
			case 0:
				out.println(0);//您要转账的卡号不存在
				break;
			case 1:
				out.println(1);//对方卡号已冻结，无法完成转账
				break;
			case 2:
				out.println(2);//您卡中的余额不足
				break;
			case 3:
				out.println(3);//转账成功
				break;
			case 4:
				out.println(4);//不可以向本卡转账
				break;
			}
			
		}else if("selectstatus".equals(instatus)){
			String cardid=(String)(request.getSession().getAttribute("cardid"));
			UserBean ub=ud.selectStatus(cardid);
			request.getSession().setAttribute("balance", ub.getBalance());
			request.getSession().setAttribute("status", ub.getStatus());
			request.getSession().setAttribute("selectstatus", ub);
			out.print(1);
		}else if("changepass".equals(instatus)){ //修改密码
			String cardid=(String)(request.getSession().getAttribute("cardid"));
			String webpass=request.getParameter("webpass");
			String webpass1=request.getParameter("webpass1");
			String webpass2=request.getParameter("webpass2");
			int i=ud.changepass(cardid, webpass, webpass1, webpass2);
			switch(i){
			case 0:
				out.println(0); //原始密码输入错误
				break;
			case 1:
				out.println(1); //两次密码输入不一致
				break;
			case 2:
				out.println(2);  //修改成功
				break;
			}
		}else if("selectuser".equals(instatus)){
			String cardid=(String)(request.getSession().getAttribute("cardid"));
			UserBean ub=ud.selectuser(cardid);
			request.getSession().setAttribute("user", ub);
			out.print(1);
		}else if("selectmingxi".equals(instatus)){
			String cardid=(String)(request.getSession().getAttribute("cardid"));
			List<MingxiBean> list=ud.selectmingxi(cardid);
			request.getSession().setAttribute("mingxi", list);
			out.print(1);
		}else if("zhaohui_one".equals(instatus)){ //找回用户密码的第一步
			String cardid=request.getParameter("cardid");
			request.getSession().setAttribute("cardid", cardid);
			UserBean ub= ud.selectuser(cardid);
			request.getSession().setAttribute("webname", ub.getWebname());
			String userid=request.getParameter("userid");
			String pass=request.getParameter("pass");
			int i=ud.zhaohui1(cardid, userid, pass);
			switch(i){
			case 0:
				out.println(0); //卡号不存在
				break;
			case 1:
				out.println(1);  //身份证号和卡号不匹配
				break;
			case 2:
				out.println(2);  //取款密码输入错误
				break;
			case 3:
				out.print(3);  //成功，进入下一步
				break;
			}
		}else if("zhaohui_two_yes".equals(instatus)){
			String newpass=request.getParameter("new_webpass");
			String confirm_newpass=request.getParameter("confirm_new_webpass");
			String cardid=(String)request.getSession().getAttribute("cardid");
			if(newpass.equals(confirm_newpass)){
				request.getSession().setAttribute("webpass", newpass);
				ud.zhaohui_pass(cardid, newpass);
				out.print(1);
			}else if(!newpass.equals(confirm_newpass)){
				out.print(0);
			}
		}else if("zhaohui_two_no".equals(instatus)){
			String pass=request.getParameter("old_webpass");
			String cardid=(String)(request.getSession().getAttribute("cardid"));
			UserBean ub=ud.selectuser(cardid);
			if(pass.equals(ub.getWebpass())){
				out.print(1);
			}else if(!pass.equals(ub.getPass())){
				out.print(0);
			}
		}else if("zhaohui3_login".equals(instatus)){
			String cardid=(String)request.getSession().getAttribute("cardid");
			String webpass=(String)request.getSession().getAttribute("webpass");
			UserBean ub=ud.login0(cardid, webpass);
			Date dt = new Date();
			SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String ltime = d.format(dt);
			int i=ud.login(cardid, webpass);
			switch(i){
			case 0:
				out.print(0); //没有该用户
				break;
			case 1:
				out.print(1); //密码输入错误
				break;
			case 2:
				request.getSession().setAttribute("cardid", ub.getCardid());
				request.getSession().setAttribute("name", ub.getName());
				request.getSession().setAttribute("balance", ub.getBalance());
				request.getSession().setAttribute("welcome", ub.getWelcome());
				request.getSession().setAttribute("logintime", ltime);
				request.getSession().setAttribute("webid", ub.getWebid());
				request.getSession().setAttribute("phone", ub.getPhone());
				out.print(2);
				break;
			case 3:
				out.print(3); //密码输入错误
				break;
			case 4:
				request.getSession().setAttribute("cardid", ub.getCardid());
				request.getSession().setAttribute("name", ub.getName());
				request.getSession().setAttribute("balance", ub.getBalance());
				request.getSession().setAttribute("welcome", ub.getWelcome());
				request.getSession().setAttribute("logintime", ltime);
				request.getSession().setAttribute("webid", ub.getWebid());
				request.getSession().setAttribute("phone", ub.getPhone());
				out.print(4);
				break;
			}
		}else if("welcome".equals(instatus)){
			String wel=request.getParameter("welcome_message");
			String cardid=(String)request.getSession().getAttribute("cardid");
			System.out.println(cardid);
			if(wel==""||wel==null){
				out.print(0); //欢迎信息不可为空
			}else{
				ud.welcome_dao(cardid, wel);
				request.getSession().setAttribute("welcome", wel);
				out.print(1);
			}
			
		}else if("bianji_user".equals(instatus)){
			String cardid=(String)request.getSession().getAttribute("cardid");
			String phone=request.getParameter("phone");
			String webname=request.getParameter("webname");
			if(phone==""||phone==null){
				out.print(0);//电话号码不可为空
			}else if(webname==""||webname==null){
				out.print(1);//用户名不得为空
			}else{
				int i=ud.update_user(cardid, phone, webname);
				if(i>0){
					request.getSession().setAttribute("phone", phone);
					request.getSession().setAttribute("webname", webname);
					out.print(2);//修改成功
				}
			}
		}
	}
}
